using System;
using Microsoft.SPOT;

namespace nRF24L10P
{
    public class DataWritePipe
    {
        nRF24L10P device;
        public DataWritePipe(nRF24L10P device)
        {
            this.device = device;
        }

        public byte[] Address
        {
            get
            {
                return device.Registers.TX_ADDR;
            }
            set
            {
                if (value.Length!=device.Config.AddressWidth)
                {
                    throw new InvalidOperationException("Address length should equal to device.Config.AddressWidth");
                }
                device.Registers.TX_ADDR.Value = value;
                device.Registers.TX_ADDR.Save();
            }
        }
        /// <summary>
        /// write data to TX FIFO, maximum buffer size is 32 bytes
        /// </summary>
        /// <param name="data"></param>
        /// <param name="disableACK"></param>
        public void Write(byte[] data, bool disableACK=false)
        {
            if (data.Length>32)
            {
                throw new ArgumentOutOfRangeException("data", "data should be 0-32 bytes");
            }
            if (disableACK)
            {
                device.SpiReadWrite(SPICommands.W_TX_PAYLOAD_NO_ACK, nRF24L10P.EMPTY_ADDRESS, data,false);
            }
            else
            {
                device.SpiReadWrite(SPICommands.W_TX_PAYLOAD, nRF24L10P.EMPTY_ADDRESS, data,false);
            }
        }

    }
}
